<?php

/**
 * Theme class
 *
 * This class provides a general API for all shopalong themes
 *
 * @author Stuart Metcalfe <shoopdev@googlemail.com>
 * @version 1.0
 */

/**
 * Theme class
 *
 */

class Theme extends Smarty
{
	var $DEFAULT = 'default';
	
	var $_author;
	var $_charSet;
	var $_description;
	var $_mimeType;
	var $_name;
	var $_title;
	var $_version;
	
	/**
	 * Constructor method
	 *
	 * @access public
	 * @param string $name If not empty, loads current theme (default empty)
	 */
	
	function Theme($name='')
	{
		global $lng;
		
		/**
		 * @todo Detect which theme is selected. This is just a hard-coded
		 * placeholder for development purposes
		 */
		
		define('CURRENT_THEME', $this->DEFAULT);
		
		if (!tep_not_null($name))
		{
			$name = CURRENT_THEME;
		}
		
		/** 
		 * @todo Some kind of error catching here.  Do we fall back to a
		 * default with a warning or do we exit with a fatal error? 
		 */
		
		$this->template_dir = DIR_FS_THEMES.$name;
		$this->compile_dir = DIR_FS_THEMES.'/compiled';
		
		/**
		 * @todo Load template data from a settings file of some kind
		 */
		
		$this->_author = 'author';
		$this->_charSet = 'UTF-8';
		$this->_description = '';
		$this->_mimeType = 'text/html';
		$this->_name = $name;
		$this->_title = 'Default Shopalong theme';
		$this->_version = '1.0';
		
		/**
		 * @todo Change entire system to 2 character language codes
		 */
		
		//$smarty->config_dir = DIR_FS_LANGUAGES.$lng->getLanguage().'/';
		$this->config_dir = DIR_FS_LANGUAGES.$lng->getLanguage().'/';
		$this->config_load('default.conf');
		$functions = get_defined_functions();
		
		foreach ($functions['user'] as $function)
		{
			// TODO: replace hardcoded prefix with a defined one
			if (substr($function,0,9) == 'template_')
			{
				$tFunction = eregi_replace('template_','',$function);
				$this->register_function($tFunction, $function);
			}
		}
	}
	
	/**
	 * Overrides Smarty's display method
	 */
	
	function display($templateName, $forceTheme='', $cacheId='', $compileId='')
	{
		$templateName = $this->getTemplateUrl($templateName, $forceTheme);
		Smarty::display($templateName, $cacheId, $compileId);
	}
	
	/**
	 * The name of the theme's author
	 *
	 * @access public
	 * @return string
	 */
	
	function getAuthor()
	{
		return $this->_author;
	}
	
	/**
	 * The theme's character set
	 *
	 * @access public
	 * @return string
	 */
	
	function getCharacterSet()
	{
		return $this->_charSet;
	}
	
	/**
	 * The theme's description
	 *
	 * @access public
	 * @return string
	 */
	
	function getDescription()
	{
		return $this->_description;
	}
	
	/**
	 * The theme's mime-type
	 *
	 * @access public
	 * @return string
	 */
	
	function getMimeType()
	{
		return $this->_mimeType;
	}
	
	/**
	 * The theme's directory name
	 *
	 * @access public
	 * @return string
	 */
	
	function getName()
	{
		return $this->_name;
	}
	
	/**
	 * Provides the correct url for the requested template
	 *
	 * Use this method in your templates to call an included template file.
	 * This method will cascade down from the current theme to the default theme
	 * until it finds the requested template.  This allows theme makers to only
	 * override the templates they want to change from the default.
	 *
	 * @access public
	 * @param string $templateName The name of the template you want to call
	 * @param string $forceTheme Forces selection of the template from the
	 * specified theme, regardless of if it exists, or if it is overridden by
	 * another theme (default empty - ignored)
	 * @return string the url for the requested template
	 */
	
	function getTemplateUrl($templateName, $forceTheme='')
	{
		// Override order: theme(local)>theme>default(local)>default
		
		/**
		 * @todo We need to find a way of managing plugin templates in the
		 * heirarchy
		 */
		
		$templates = array
		(
			DIR_FS_LOCAL.'content/themes/'.$this->getName().'/'.$templateName,
			$templateName,
			DIR_FS_LOCAL.'content/themes/default/'.$templateName,
			DIR_FS_THEMES.'default/'.$templateName
		);
		
		foreach ($templates as $template)
		{
			if ($this->template_exists($template))
			{
				return $template;
			}
		}
		
		// If we get here then no template was found!
		// Return the original template and let the theme engine handle failure
		return $templateName;
	}
	
	/**
	 * The theme's displayable title
	 *
	 * @access public
	 * @return string
	 */
	
	function getTitle()
	{
		return $this->_title;
	}
	
	/**
	 * The theme's version
	 *
	 * @access public
	 * @return string
	 */
	
	function getVersion()
	{
		return $this->_version;
	}
	
	/**
	 * Is this theme the current theme?
	 *
	 * @access public
	 * @return boolean
	 */
	
	function isCurrent()
	{
		return ($this->_name == CURRENT_THEME);
	}
	
	/**
	 * Is this theme the default theme?
	 *
	 * @access public
	 * @return boolean
	 */
	
	function isDefault()
	{
		return ($this->_name == $this->DEFAULT);
	}
}

?>
